package pl.register.android.activities.correlation.builder;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import pl.register.android.R;
import pl.register.android.activities.correlation.logic.Correlation;
import pl.register.android.activities.correlation.logic.CorrelationStudentsPresenter;
import pl.register.android.activities.correlation.logic.SelectedStudentsHolder;
import pl.register.android.activities.correlation.ui.StudentsChoiceListAdapter;
import pl.register.android.group.StudentsGroup;
import pl.register.android.student.Student;

import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-12
 */
public class StudentChooserDialogBuilder extends AlertDialog.Builder
        implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    private CorrelationStudentsPresenter presenter;

    private ListView lvStudents;

    private RadioGroup rgGroups;

    private StudentsChoiceListAdapter listAdapter;


    private StudentsGroup selectedGroup;

    private SelectedStudentsHolder selectedStudentsHolder;

    public StudentChooserDialogBuilder(Context context, CorrelationStudentsPresenter presenter) {
        super(context);
        this.presenter = presenter;
        this.selectedStudentsHolder = new SelectedStudentsHolder();

        onCreateContent(context);
        populateViews(context, presenter.getDisplayCorrelation());
        setupDialog();
    }

    private void onCreateContent(Context ctx) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_student_chooser, null);
        listAdapter = new StudentsChoiceListAdapter(ctx, selectedStudentsHolder);
        rgGroups = (RadioGroup) dialogLayout.findViewById(R.id.students_group_radio_group);
        lvStudents = (ListView) dialogLayout.findViewById(R.id.students_list_view);
        lvStudents.setAdapter(listAdapter);
        lvStudents.setOnItemClickListener(this);
        setView(dialogLayout);
    }

    private void populateViews(Context context, Correlation correlation) {
        List<Student> studentsWithoutGroup = presenter.getStudentsWithoutGroup();
        listAdapter.refreshItems(studentsWithoutGroup);
        List<StudentsGroup> groups = correlation.getGroups();
        int index = 0;
        for (StudentsGroup group : groups) {
            RadioButton radioButton = createRadioButtonBy(context, group);
            rgGroups.addView(radioButton, index);
            radioButton.setChecked(index == 0);
            index++;
        }
    }

    private RadioButton createRadioButtonBy(Context context, StudentsGroup group) {
        RadioButton radioButton = new RadioButton(context);
        radioButton.setPadding(45, 10, 10, 10);
        radioButton.setText(group.getType().getName());
        radioButton.setTag(group);
        radioButton.setOnCheckedChangeListener(this);
        return radioButton;
    }

    private void setupDialog() {
        setTitle("Przydzielanie kursantów do grupy");
        setNegativeButton(R.string.btn_name_cancel, null);
        setPositiveButton(R.string.btn_name_add, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (Student student : selectedStudentsHolder.getStudents()) {
                    presenter.addStudentToGroup(student, selectedGroup);
                }
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Student student = listAdapter.getItem(position);
        listAdapter.toggleItem(view);
        actualizeHolder(listAdapter.isChecked(view), student);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            selectedGroup = (StudentsGroup) buttonView.getTag();
        }
    }

    private void actualizeHolder(boolean isChecked, Student student) {
        if (isChecked) {
            selectedStudentsHolder.add(student);
        } else {
            selectedStudentsHolder.remove(student);
        }
    }
}